Can somebody give me a hand with this? I'm doing this for a an application that would print of LCM and GCD (lowest common factor and greatest common divisor) with steps.
I need to get the common factors so I can highlight them when I'm printing them under factors. I tried googling but I could only find programs that would directly find LCM or GCD. It's important I have all the factors.
The best I could come up with:
Code:
int main() {
vector <float> operands = { 9, 9, 4 };
vector < vector <float> > factors;
vector < vector <float> > factors_temp;
vector <float> common_factors;
for (const auto& x : operands)
factors.push_back(prime_factors(x)); // returns a vector
factors_temp = factors;
for (unsigned int i = 0; i < factors_temp.size(); i++) {
for (unsigned int j = 0; j < factors_temp[i].size(); j++) {
for (unsigned int k = 0; k < factors_temp.size(); k++) {
for (unsigned int l = 0; l < factors_temp[k].size(); l++) {
if (k == i)
continue;
if (factors_temp[i][j] == factors_temp[k][l]) {
common_factors.push_back(factors_temp[i][j]);
factors_temp[i].erase(factors_temp[i].begin() + j);
factors_temp[k].erase(factors_temp[k].begin() + l);
}
}
}
}
}
for (const auto& x : common_factors)
cout << x << "\t";
cin.get();
return 0;
}
The logic is completely faulty though. It occasionally works for two operands.
Oh and also I need to erase common factors from the rows and columns themselves. (So they won't repeat while printing out, unless there is a better logic to this)
Thanks for reading, cheers!
here's the other part of the code:
Code:
#include <iostream>
#include <vector>
#include <string>
#include <Windows.h>
using namespace std;
void SetColor(int ForgC) // source: https://stackoverflow.com/questions/29574849/how-to-change-text-color-and-console-color-in-codeblocks
{
WORD wColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for the wAttributes word.
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
vector <int> prime_factors(int number) { // over-loaded function
vector <int> prime_factors = { 1 };
for (int i = 2; i <= number; i++) {
while (number % i == 0) {
number /= i;
prime_factors.push_back(i);
}
}
return prime_factors;
}